#include<sys/mman.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

/*
 *
read=123456789012345678901234567890
read=1234567890123456789012345678901234567890
read=123456789012345678901234567890123456789012345678901234567890
 */

int main(){
	void * addr;

	//mmap创建共享内存
	addr = mmap(NULL, 2048, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
	if(addr == MAP_FAILED){
		perror("mmap");
		return 0;
	}

	pid_t pid = fork();
	if(pid < 0){
		perror("fork");
		return -1;
	}else if(pid == 0){
		//子线程 读
		while(1){
			sleep(1);
			printf("read=%s\n",(char *)addr);
		}
	}else{
		//父线程 写
		int i = 0;
		while(1){
			memcpy(addr, "1234567890", 10);
			addr = addr + 10;
			usleep(500000);
		}
	}

	//释放内存映射
	munmap(addr, 2048);


}
